<?php

// Each call to jQuery assigns a new unique id to that object, and if that object is not referenced anywhere else, it is inserted as a standalone statement. if it is referenced elsewhere, it is assigned to a variable in js

function &j($selector, $context = null) {
	return new jQuery($selector, $context);
}

function &jQuery($selector, $context = null) {
	return new jQuery($selector, $context);
}

class jQuery implements ArrayAccess {
	private static $_counter = 0;
	private static $_buffer = null;
	private static $_vars = array();

	private $_var_name = null;
	private $_selector = null;
	private $_context = null;
	private $_chain = array();

	public static function StartBuffering() {
		self::$_buffer = array();
	}

	public static function GetBufferedCode() {
		if (is_array(self::$_buffer) && count(self::$_buffer)) {
			return implode(';', self::$_buffer) . ';';
		} else {
			return false;
		}
	}

	function __construct($selector, $context = null) {
		$this->_selector = $selector;
		$this->_context = $context;
		$this->_var_name = 'j_' . uniqid();
		self::$_vars[$this->_var_name] = $this;

		if (is_array(self::$_buffer)) {
			self::$_buffer[] = $this;
		}
	}

	function __call($method, $args) {
		$args = self::_implodeArgs($args);
		$this->_chain[] = ".$method($args)";
		return $this;
	}

	function __get($name) {
		$this->_chain[] = ".$name";
		return $this;
	}

	function __set($name, $value) {
		if (is_scalar($value)) {
			$this->_chain[] = ".$name = " . self::_quoteSlash($value);
		} else if ($value instanceof jQuery) {
			$this->_chain[] = (string)$value;
		}

		return $this;
	}

	function offsetExists($offset) {
		//
	}

	function offsetGet($offset) {
		$this->_chain[] = "[" . self::_quoteSlash($offset) . "]";
		return $this;
	}

	function offsetSet($offset, $value) {
		$this->_chain[] = "[$offset] = " . self::_quoteSlash($value);
	}

	function offsetUnset($offset) {
		// "delete $thisObj[$offset];\n";
	}

	function __tostring() {
		//$selector = $this->_selector instanceof jQuery ? $this->_selector : "'{$this->_selector}'";
		$selector = self::_quoteSlash($this->_selector);
		$chain = implode('', $this->_chain);

		if (is_null($this->_context)) {
			return "$($selector)$chain";
		} else {
			//$context = $this->_context instanceof jQuery ? $this->_context : self::_quoteSlash($this->_context);
			$context = self::_quoteSlash($this->_context);
			return "$($selector,$context)$chain";
		}
	}

	private static function _implodeArgs(array $args) {
		$outputArgs = array();

		foreach ($args as $arg) {
			if (is_scalar($arg)) {
				$outputArgs[] = is_string($arg) ? self::_quoteSlash($arg) : $arg;
			} else if($arg instanceof jQuery) {
				$outputArgs[] = (string)$arg;
			}
		}

		return implode(',', $outputArgs);
	}

	private static function _quoteSlash($value) {
		return $value instanceof jQuery ? (string)$value : (is_string($value) ? "'" . addslashes($value) . "'" : $value);
	}
}

class jQueryBrowserContext extends jQuery {
	private $html = '';
	private $text = '';
	private $val = '';

	public function __construct($html, $text, $val, $children) {
		
	}

	public function html() {
		func_num_args() == 0 ? $this->_getHtml() : $this->_setHtml(func_get_arg(1));
	}

	private function getHtml() {
		
	}

	private function setHtml($arg) {
		
	}

	public function text() {
		func_num_args() == 0 ? $this->_getText() : $this->_setText( func_get_arg( 1 ) );
	}

	private function getText() {
		
	}

	private function setText($arg) {
		
	}

	public function val() {
		func_num_args() == 0 ? $this->_getVal() : $this->_setVal(func_get_arg(1));
	}

	private function getVal() {
		
	}

	private function setVal($arg) {
		
	}

}
